#pragma once
#include "operate.hpp"
#include "boost/algorithm/string.hpp"
#include "/home/pigff/project/search_engine/search_engine.h"

#define COUNTPERPAGE 10
#define SHOWPAGE 5 
class Action{
public:
    //返回主界面
    static void ActionIndex(MYSQL* conn,string& output,string user){
        TemplateDictionary dict("index");
        dict.ShowSection("TEST_TABLE");

        TemplateDictionary *item = dict.AddSectionDictionary("TEST_TABLE_ITEM");
        if(user == "")
            item->SetValue("LABEL", "<li><a href=\"login.html\">登录</a></li>\n \
                           <li><a href=\"logon.html\">注册</a></li>");
        else{
            string tmp = "<li><a href=\"modify.html\">你好！";
            tmp += user + "</a></li> <li><a href=\"cgi_logout?user=\">注销</a></li>";
            item->SetValue("LABEL", tmp);
        }
        std::vector<string> topics = OperateTopic::AllTopic(conn);
        for (auto i:topics) {
            item = dict.AddSectionDictionary("TEST_TABLE_ITEM1");
            item->SetValue("NAME", i);
        }

        std::vector<string> news = OperateNews::LatestTenNews(conn);
        for (auto i:news) {
            item = dict.AddSectionDictionary("TEST_TABLE_ITEM2");
            item->SetValue("TITLE", i);
        }

        Template* tpl = Template::GetTemplate("./MyRootDir/index.tpl",DO_NOT_STRIP);
        tpl->Expand(&output,&dict);

        //设置一个cookie
        cout << "Set-Cookie: action=index;\n"; 
    }

    static void ActionState(MYSQL* conn,string& output,string user,string pwd,string what){
        TemplateDictionary dict("state");
        dict.ShowSection("TEST_TABLE");
        
        TemplateDictionary *item = dict.AddSectionDictionary("TEST_TABLE_ITEM");
        
        if(OperateUser::IsUserExist(conn,user,pwd)){
            if(what == "login"){
                item->SetValue("STATE", "登录成功！");
                item->SetValue("state", "cgi_login?state=success");
                cout << "Set-Cookie: user=" << user << ";\n";
            }else{
                item->SetValue("STATE", "注册失败，用户名重复！");
                item->SetValue("state", "logon.html");
                cout << "Set-Cookie: user=" << ";\n";
            }
        }
        else{
            if(what == "login"){
                item->SetValue("STATE","登录失败，用户名或密码错误");
                item->SetValue("state", "login.html");
                cout << "Set-Cookie: user=" << ";\n";
            }else{
                item->SetValue("STATE", "注册成功");
                item->SetValue("state", "cgi_logon?state=success");
                cout << "Set-Cookie: user=" << user << ";\n";
                cout << "Set-Cookie: cgi_pwd=" << pwd << ";\n"; 
            }
        }

        Template* tpl = Template::GetTemplate("./MyRootDir/state.tpl",DO_NOT_STRIP);
        tpl->Expand(&output,&dict);
    }
    
    //返回管理员界面
    static void ActionAdmin(MYSQL* conn,string& output){
        FILE* fp = fopen("./MyRootDir/admin.html","r");
        if(!fp)
            return;

        char c = 0;
        while(!feof(fp)){
            c = fgetc(fp);
            // if(c == '@'){
            //     std::vector<string> ret = OperateNews::LatestTenNews(conn);
            //     for(auto i : ret){
            //         string ret1 = "<center><a href=\"#\" class=\"btn btn-success btn-lg btn-link\">";
            //         string ret2 = "</a></center>\n";
            //         output += ret1 + i + ret2;
            //     }
            // }
            // else
                output += c;
        }
    }

    //返回display页面
    static void ActionDisplay(MYSQL* conn,string& output,string topic,string user,string page){

        int topicid = OperateTopic::IdOfTopic(conn,topic);
        std::vector<string> titles,authors,createdates;
        OperateNews::GetNew1(conn,topicid,titles,authors,createdates);

        TemplateDictionary dict("display");
        dict.ShowSection("TEST_TABLE");

        TemplateDictionary *item;
        std::vector<string> topics = OperateTopic::AllTopic(conn);
        for (auto i:topics) {
            item = dict.AddSectionDictionary("TEST_TABLE_ITEM");
            item->SetValue("TOPIC", i);
        }

        item = dict.AddSectionDictionary("TEST_TABLE_ITEM1");
        if(user == "")
            item->SetValue("LABEL", "<li><a href=\"login.html\">登录</a></li>\n \
                           <li><a href=\"logon.html\">注册</a></li>");
        else{
            string tmp = "<li><a href=\"modify.html\">你好！";
            tmp += user + "</a></li> <li><a href=\"cgi_logout?user=\">注销</a></li>";
            item->SetValue("LABEL", tmp);
        }
        
        //总共有count个新闻
        int count = titles.size();
        //规定每页有多少个新闻
        int countperpage = COUNTPERPAGE;
        //计算出总共有多少页
        int pages = count / countperpage;
        if(pages * countperpage != count)
            pages += 1;

        int i = (stoi(page) - 1) * countperpage;
        if(titles.size() == 0){
            item = dict.AddSectionDictionary("TEST_TABLE_ITEM2");
            item->SetValue("STATE","#");
        }else{
            while(countperpage--&&i < count){
                item = dict.AddSectionDictionary("TEST_TABLE_ITEM2");
                item->SetValue("STATE","cgi_newsforuser?title=");
                item->SetValue("TITLE",titles[i]);
                item->SetValue("AUTHOR", authors[i]);
                item->SetValue("DATE", createdates[i]);
                i++;
            }
        }
        
        //替换上一页
        int showpage = SHOWPAGE;
        item = dict.AddSectionDictionary("TEST_TABLE_ITEM3");
        if((stoi(page) -1) == 0){
            item->SetValue("STATE1","class=\"disabled\">");
            item->SetValue("HREF1","#");
        }else{
            item->SetValue("STATE1","");
            string info = "cgi_display?topic=";
            info += topic;
            info += "&page=";
            info += std::to_string(stoi(page)-1);
            item->SetValue("HREF1",info);
        }

        //替换下一页
        if((stoi(page) + 1) > pages){
            item->SetValue("STATE2","class=\"disabled\">");
            item->SetValue("HREF2","#");
        }else{
            item->SetValue("STATE2","");
            string info = "cgi_display?topic=";
            info += topic;
            info += "&page=";
            info += std::to_string(stoi(page) + 1);
            item->SetValue("HREF2",info);
        }

        //替换首页和尾页
        string tmp = "cgi_display?topic=";
        tmp += topic;
        item->SetValue("CGI",tmp);
        if(pages == 0)
            item->SetValue("LASTPAGE","1");
        else
            item->SetValue("LASTPAGE",std::to_string(pages));
        item->SetValue("CUR",page);

        int j = 1; 
        while(showpage > 0 && (stoi(page) + j <= pages )){
            item = dict.AddSectionDictionary("TEST_TABLE_ITEM4");
            string info = "cgi_display?topic=";
            info += topic;
            LOG(INFO) << info << endl;
            item->SetValue("CGI",info);
            item->SetValue("PAGE",std::to_string(stoi(page) + j));
            showpage--;
            ++j;
        }
        
        Template* tpl = Template::GetTemplate("./MyRootDir/display.tpl",DO_NOT_STRIP);
        tpl->Expand(&output,&dict);

        cout << "Set-Cookie: action=display;\n"; 
    }

    static void ActionSearch(MYSQL* conn,string& output,string keyword,string user,string page){
        std::vector<string> titles,authors,createdates;
        
        TemplateDictionary dict("display");
        dict.ShowSection("TEST_TABLE");

        TemplateDictionary *item;
        std::vector<string> topics = OperateTopic::AllTopic(conn);
        for (auto i:topics) {
            item = dict.AddSectionDictionary("TEST_TABLE_ITEM");
            item->SetValue("TOPIC", i);
        }

        item = dict.AddSectionDictionary("TEST_TABLE_ITEM1");
        if(user == "")
            item->SetValue("LABEL", "<li><a href=\"login.html\">登录</a></li>\n \
                           <li><a href=\"logon.html\">注册</a></li>");
        else{
            string tmp = "<li><a href=\"modify.html\">你好！";
            tmp += user + "</a></li> <li><a href=\"cgi_logout?user=\">注销</a></li>";
            item->SetValue("LABEL", tmp);
        }
        
        //总共有count个新闻
        int count = titles.size();
        //规定每页有多少个新闻
        int countperpage = COUNTPERPAGE;
        //计算出总共有多少页
        int pages = count / countperpage;
        if(pages * countperpage != count)
            pages += 1;

        int i = (stoi(page) - 1) * countperpage;
        
        if(titles.size() == 0){
            item = dict.AddSectionDictionary("TEST_TABLE_ITEM2");
            item->SetValue("STATE","#");
        }else{
            while(countperpage--&&i < count){
                item = dict.AddSectionDictionary("TEST_TABLE_ITEM2");
                item->SetValue("STATE","cgi_newsforuser?title=");
                item->SetValue("TITLE",titles[i]);
                item->SetValue("AUTHOR", authors[i]);
                item->SetValue("DATE", createdates[i]);
                i++;
            }
        }
        
        int showpage = SHOWPAGE;
        item = dict.AddSectionDictionary("TEST_TABLE_ITEM3");
        if((stoi(page) -1) == 0){
            item->SetValue("STATE1","class=\"disabled\">");
            item->SetValue("HREF1","#");
        }else{
            item->SetValue("STATE1","");
            string info = "cgi_userSearch?keyword=";
            info += keyword;
            info += "&page=";
            info += std::to_string(stoi(page)-1);
            item->SetValue("HREF1",info);
        }
        item->SetValue("CUR",page);

        if((stoi(page) + 1) > pages){
            item->SetValue("STATE2","class=\"disabled\">");
            item->SetValue("HREF2","#");
        }else{
            item->SetValue("STATE2","");
            string info = "cgi_userSearch?keyword=";
            info += keyword;
            info += "&page=";
            info += std::to_string(stoi(page) + 1);
            item->SetValue("HREF2",info);
        }
 
        //替换首页和尾页
        string tmp = "cgi_userSearch?keyword=";
        tmp += keyword;
        item->SetValue("CGI",tmp);
        if(pages == 0)
            item->SetValue("LASTPAGE","1");
        else
            item->SetValue("LASTPAGE",std::to_string(pages));
        item->SetValue("CUR",page);

        int j = 1; 
        while(showpage > 0 && (stoi(page) + j <= pages )){
            item = dict.AddSectionDictionary("TEST_TABLE_ITEM4");
            string info = "cgi_userSearch?keyword=";
            info += keyword;
            item->SetValue("CGI",info);
            item->SetValue("PAGE",std::to_string(stoi(page) + j));
            showpage--;
            ++j;
        }
        

        Template* tpl = Template::GetTemplate("./MyRootDir/display.tpl",DO_NOT_STRIP);
        tpl->Expand(&output,&dict);

        cout << "Set-Cookie: action=search;\n"; 
    }

    //返回具体某个新闻的页面
    static void ActionNews(MYSQL* conn,string& output,string& title,string& comment,string user){
        string author,date,content,id;
        OperateNews::GetNew2(conn,title,author,date,content,id);

        string newcontent;
        //将内容中的'\n'换成html中的换行符"<br>"
        for(auto i:content){
            if(i == '\n')
                newcontent += "<br>";
            else
                newcontent += i;
        }

        //comment表示新增加的评论，如果不为空就先加上评论,并且必须是用户才可以添加评论
        if(!comment.empty() && !user.empty())
            OperateComment::AddComment(conn,id,comment,user);

        std::vector<string> comments;
        OperateComment::GetComment(conn,id,comments);

        TemplateDictionary dict("shownews");
        dict.ShowSection("TEST_TABLE");
        
        TemplateDictionary *item;
        std::vector<string> topics = OperateTopic::AllTopic(conn);
        for (auto i:topics) {
            item = dict.AddSectionDictionary("TEST_TABLE_ITEM");
            item->SetValue("TOPIC", i);
        }

        item = dict.AddSectionDictionary("TEST_TABLE_ITEM1");
        if(user == "")
            item->SetValue("LABEL", "<li><a href=\"login.html\">登录</a></li>\n \
                           <li><a href=\"logon.html\">注册</a></li>");
        else{
            string tmp = "<li><a href=\"modify.html\">你好！";
            tmp += user + "</a></li> <li><a href=\"cgi_logout?user=\">注销</a></li>";
            item->SetValue("LABEL", tmp);
        }

        if(true) {
            TemplateDictionary *item = dict.AddSectionDictionary("TEST_TABLE_ITEM2");
            item->SetValue("TITLE",title);
            item->SetValue("AUTHOR", author);
            item->SetValue("DATE", date);
            item->SetValue("CONTENT", newcontent);
            item->SetValue("USER", user);
        }

        for(auto i:comments){
            TemplateDictionary *item = dict.AddSectionDictionary("TEST_TABLE_ITEM3");
            item->SetValue("COMMENT",i);
        }

        Template* tpl = Template::GetTemplate("./MyRootDir/newsforuser.tpl",DO_NOT_STRIP);
        tpl->Expand(&output,&dict);

        cout << "Set-Cookie: action=news;\n"; 
    }
};

